package com.briup.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.common.util.Result;
import com.briup.system.pojo.SysUser;
import com.briup.system.service.SysUserService;
import com.ctc.wstx.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    // 这里返回的时候使用的工具类要提前定义
    @PostMapping("/saveOrUpdate")
    public Result<String> saveOrUpdateUser(@RequestBody SysUser user){
        // 根据实际情况，对user做简单处理
        // 新增的时候
        // role的名字不需要用户填入，状态也不需要用户填入，密码可以提前设置默认的
        // 只需要填，用户名字和角色ID
        boolean b = sysUserService.saveOrUpdate(user);
        if(b){
            return Result.success("操作成功",null,1);
        }
        return Result.fail();
    }
    // 删除逻辑分为批量删除和单个删除，那么可写一个接口，也可以写两个接口
    @DeleteMapping("/deleteById/{userId}")
    public Result<String> deleteById(@PathVariable Long userId){
        // 删除的时候要考虑是逻辑删除还是物理删除
        // 物理删除
        boolean b = sysUserService.removeById(userId);
        // 逻辑删除做的是更新操作（后续补上）
        if(b){
            return Result.success("删除成功",null,1);
        }
        return Result.fail();
    }

    @DeleteMapping("/deleteBatch")
    public Result<String> deleteBatch(List<Long> ids){
        boolean b = sysUserService.removeBatchByIds(ids);
        if(b){
            return Result.success("删除成功",null,ids.size());
     }
        return Result.fail();
    }


    @ApiOperation(value = "查询接口",notes = "集合了查询全部、用户名模糊、角色筛选的分页查询和不分页查询")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "pageNum",required = true),
                    @ApiImplicitParam(name = "pageSize",value = "每页数据的条数，如果是负数，默认为查询全部",required = true),
                    @ApiImplicitParam(name = "username",required = false),
                    @ApiImplicitParam(name = "roleId",required = false)
            }
    )
    @GetMapping("/selectAllByPage")
    public Result<Page<SysUser>> selectUserList(@RequestParam int pageNum,
                                                @RequestParam int pageSize,
                                                @RequestParam(required = false) String username,
                                                @RequestParam(required = false) Long roleId){
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        // 如果username不为空，做模糊查询
        if(!StringUtils.isBlank(username)){
            wrapper.like("usr_name","%"+username+"%");
        }
        // 根据 roleId 是否存在来进行角色筛选查询
        if(roleId != null){
            wrapper.eq("usr_role_id",roleId);
        }
        // 根据pageSize的大小来判断是否分页
        // 如果pageSize小于0，则默认就不分页了
        Page<SysUser> page = sysUserService.page(new Page<SysUser>(pageNum,pageSize),wrapper);
        List<SysUser> records = page.getRecords();
        // return Result.success("查询成功",records,records.size());
        return Result.success("查询成功",page,records.size());

    }

    @ApiOperation("根据用户名密码查询单个用户对象")
    @PostMapping("/findUserByName")
    public Result<SysUser> selectUserByUsernameAndPassword(@RequestBody Map<String,String> map){
        String username = map.get("username");
        String password = map.get("password");
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("usr_name",username).eq("usr_password",password);
        SysUser user = sysUserService.getOne(queryWrapper);
        if(user!=null){
            return Result.success("查询成功",user,1);
        }
        return Result.fail();
    }



}
